package com.jtyoui.web;


import com.jtyoui.code.Holidays;
import com.jtyoui.code.Lunar;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Scanner;

/**
 * 分析+扩展集合类
 *
 * @author Tyoui
 * @version 1.8.1
 */
public class Run implements Runnable {
    private int port;
    private Holidays holidays = new Holidays();

    private Run(int port) {
        this.port = port;
    }

    /**
     * 启动服务
     *
     * @param serverSocket 开启服务
     */
    private void server(ServerSocket serverSocket) {
        Socket socket = null;
        try {
            socket = serverSocket.accept();
            Scanner scanner = new Scanner(socket.getInputStream());
            String text = "";
            if (scanner.hasNext())
                text = scanner.nextLine();
            if (text.contains("icon.ico"))
                return;
            String date = text.substring(text.indexOf('/') + 4, text.indexOf('H') - 1);
            Calendar calendar = Lunar.getLunar(date);
            Lunar lunar = new Lunar(calendar);
            String result = lunar.toString();
            String d[] = result.split(" ");
            String one = holidays.getHoliday(d[1] + "月" + d[2]);
            if (one != null)
                result += " " + one;
            String two = holidays.getHoliday(date.substring(date.indexOf('-') + 1));
            if (two != null)
                result += " " + two;
            if (one == null && two == null)
                result += " 无";
            sendMessageURL(socket, result);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            while (true) {
                server(serverSocket);
                Thread.sleep(10);
            }
        } catch (Exception e) {
            try {
                if (serverSocket != null)
                    serverSocket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    private void sendMessageURL(Socket socket, String text) {
        PrintStream out = null;
        try {
            out = new PrintStream(socket.getOutputStream());
            out.println("HTTP/1.1 200 OK");
            out.println("Content-type: application/json;");
            out.println();
            out.print(text);
            out.flush();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (out != null)
                out.close();
        }
    }

    public static void main(String[] args) {
        new Run(8080).run();
    }

}